package com.phuc.game.andPuzzle.common;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;

public class Utils {
	public static void loadImageAssets(Context mContext,ImageView img, String position){
        InputStream ims;
		try {
			Log.d("Chung","position:"+ position);
			ims = mContext.getAssets().open("gfx/" + position+".jpg");
	        // load image as Drawable
	        Drawable d = Drawable.createFromStream(ims, null);
	        // set image to ImageView
	        img.setImageDrawable(d);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static double getRotationFromLine(
			float x1, float y1, float x2, float y2) {
		double rotation = Math.atan((
				(double)x1 - (double)x2)
				/ ((double)y2 - (double)y1));
		double rotationDeg = Math.toDegrees(rotation);
		return rotationDeg;
	}

	public static void unbindDrawable(View view) {
		if (view.getBackground() != null) {
			view.getBackground().setCallback(null);
			view.setBackgroundDrawable(null);
		}
		
		if (view instanceof ImageView) {
			ImageView img = (ImageView) view;
			try {
				if (img.getDrawable() != null) {
					Bitmap bmp = ((BitmapDrawable)img.getDrawable()).getBitmap();
					bmp.recycle();
				}
			} catch(Exception ex) { }
			
			img.setImageBitmap(null);
		}
		
		if (view instanceof AdapterView) {
			AdapterView viewGroup = (AdapterView) view;
			for (int i = viewGroup.getChildCount() - 1; i > -1; i--) {
				unbindDrawable(viewGroup.getChildAt(i));
			}
			return;
		}
		
		if (view instanceof ViewGroup) {
			ViewGroup viewGroup = (ViewGroup) view;
			for (int i = viewGroup.getChildCount() - 1; i > -1; i--) {
				unbindDrawable(viewGroup.getChildAt(i));
			}
			viewGroup.removeAllViews();
		}
	}
	
	public static void savePreferenceInt(String key, int value, Activity activity) {
		SharedPreferences.Editor editor = activity.getPreferences(Context.MODE_PRIVATE).edit();
		editor.putInt(key, value);
		editor.commit();
	}
	
	public static int getPreferenceInt(String key, int defaultValue, Activity activity) {
		SharedPreferences prefs = activity.getPreferences(Context.MODE_PRIVATE);
		return prefs.getInt(key, defaultValue);
	}
	
	public static void savePreferenceString(String key, String value, Activity activity) {
		SharedPreferences.Editor editor = activity.getPreferences(Context.MODE_PRIVATE).edit();
		editor.putString(key, value);
		editor.commit();
	}
	
	public static String getPreferenceString(String key, Activity activity) {
		SharedPreferences prefs = activity.getPreferences(Context.MODE_PRIVATE);
		return prefs.getString(key, "");
//		return "1;2;3;1;2;1;1;1;1;1;1;1;1;1;1;2;2;2;2;2;3;3;3;3;3;2;1;3;1;3;2;3;1";
	}
	
	public static boolean isNullOrEmpty(String input) {
		if (input == null) {
			return true;
		}
		
		
		if ("".equals(input.trim())) {
			return true;
		}
		return false;
	}
}
